package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.FeedBackBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2016/12/20.
 */
public class GetDatumAdapter extends BaseAdapter {
    private Context context;
    private List<FeedBackBean.InfoEntity> info1 = new ArrayList<>();
    private View.OnClickListener listener;

    public GetDatumAdapter(Context context, List<FeedBackBean.InfoEntity> info1, View.OnClickListener listener) {
        this.context = context;
        this.info1 = info1;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return info1.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        GetDatumViewHolder viewHolder = new GetDatumViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.get_datum_item, null);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tv_number = (EditText) convertView.findViewById(R.id.tv_number);
            viewHolder.reduce = (ImageView) convertView.findViewById(R.id.reduce);
            viewHolder.iv_add = (ImageView) convertView.findViewById(R.id.iv_add);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GetDatumViewHolder) convertView.getTag();
        }
        viewHolder.tv_number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                try {
                    Integer integer = Integer.parseInt(s + "");
                    info1.get(position).setData_nums(integer + "");
                } catch (Exception e) {
                    info1.get(position).setData_nums("0");
                }
                // notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        viewHolder.tv_title.setText(info1.get(position).getData_name());
        viewHolder.tv_number.setText(info1.get(position).getData_nums());

        viewHolder.reduce.setOnClickListener(listener);
        viewHolder.reduce.setTag(position);
        viewHolder.iv_add.setOnClickListener(listener);
        viewHolder.iv_add.setTag(position);
        return convertView;
    }

    class GetDatumViewHolder {
        TextView tv_title;
        EditText tv_number;
        ImageView reduce;
        ImageView iv_add;
    }
}
